package lwjgl.engine;

import java.util.Vector;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import datastructures.Vector2f;
import interfaces.IMovable;

public class MovableSprite extends Sprite implements IMovable {

	private Vector2f position;
	private Vector<Vector2f> waypoints;
	private float speed;
	private Vector2f movedir;
	private boolean isMoving = false;
	
	public MovableSprite(Game g, String ref,Vector2f position, float speed) {
		super(g, ref);
		this.position = position;
		this.speed = speed;
	}

	@Override
	public double getRange() {
		return 0;
	}
	
	@Override
	public int move(Vector2f p) {
		return 0;
	}

	@Override
	public double getFuelAmount() {
		return 0;
	}

	@Override
	public double refuel(double amount) {
		return 0;
	}

	@Override
	public Vector2f getPosition() {
		synchronized (position) {
			return this.position;
		}
	}

	@Override
	public Vector<Vector2f> getWaypoints(){
		synchronized (waypoints) {
			return waypoints;
		}
	}

	@Override
	public synchronized void appendWaypoints(Vector<Vector2f> waypoints)
	{
		synchronized (waypoints) {
			this.waypoints.addAll(waypoints);
		}
		this.isMoving = true;
	}

	@Override
	public synchronized void setWaypoints(Vector<Vector2f> waypoints)
	{
		synchronized (waypoints) {
			this.waypoints = waypoints;
		}
		this.isMoving = true;
	}	
	
	@Override
	protected void update(long gametime) {
		if(isMoving)
		{
			this.position.add(movedir.scale_cpy(speed*(gametime/100)));
			
			if(this.position.isInRange(this.waypoints.firstElement(), movedir.length()))
			{
				this.waypoints.remove(0);
				if(!waypoints.isEmpty())
				{
					this.movedir = Vector2f.subtract(this.waypoints.firstElement(), position);
					this.movedir.normalize();
				}
				else
				{
					this.isMoving = false;
				}
			}
			
		}
	}
	
	public static MovableSprite MovableSpriteCreator()
	{
		return null;
	}

	@Override
	public void stopMoving() {
		this.isMoving = false;
	}

	@Override
	public void continueMoving() {
		synchronized (waypoints) {
			if (!this.waypoints.isEmpty())
			{
				this.isMoving = true;
			}
		}
	}



}
